<?php

namespace app\controller\admin;

use app\common\model\StudentModel;
use app\model\AdminAttendanceRecord;
use app\model\AdminAttendanceSys;
use app\model\AdminStaff;
use app\model\AdminStatisticsAttendance;
use app\util\ReturnCode;
use think\Response;

class StatisticsAttendance extends Base {

    /**
     * 获取考勤列表
     */
    public function getAttendanceList(): Response {
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $grade = $this->request->get('grade', '');
        $scope = $this->request->get('scope', '');

        $obj = AdminAttendanceSys::where('is_delete', 0);
        if ($school_id > 0) {
            $obj->where('school_id', $school_id);
        }
        if ($scope) {
            $obj->where('scope', $scope);
        }
        if ($grade) {
            $obj->whereRaw("FIND_IN_SET({$grade}, grade)");
        }
        $list = $obj->with('school')
            ->append(['date'])
            ->order('create_time', 'DESC')->select();

        return $this->buildSuccess([
            'list'  => $list,
            'grade_list' => StudentModel::gradeList()
        ]);
    }

    /**
     * 获取考勤统计数据
     */
    public function getStatisticsList(): Response {
        $attendance_id = $this->request->get('attendance_id');
        if (!$attendance_id) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '请选择考勤组');
        }
        $start_time = $this->request->get('start_time');
        $end_time = $this->request->get('end_time');
        if (!$start_time || !$end_time) {
            $start_time = date("Y-m-d", strtotime("-7 day"));
            $end_time = date('Y-m-d');
        }
        $attendanceSys = AdminAttendanceSys::where('id','=', $attendance_id)->find();
        if (!$attendance_id) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '考勤组不存在');
        }
        $attendanceSysData = TimerTask::statisticsAttendanceData($attendanceSys,'today'); // 实时数据

        $list = AdminStatisticsAttendance::where('attendance_id','=', $attendance_id)
            ->where('statistics_time', '>=', strtotime($start_time.' 00:00:00'))
            ->where('statistics_time', '<=', strtotime($end_time.' 23:59:59'))
            ->order('statistics_time', 'ASC')
            ->select();
        foreach ($list as $value) {
            $value['statistics_time'] = date('Y-m-d', $value['statistics_time']);
        }
        return $this->buildSuccess([
            'list' => $list,
            'now' => $attendanceSysData
        ]);
    }

    /**
     * 获取考勤统计学生数据
     */
    public function getStatisticsStudentList(): Response {
        $attendance_id = $this->request->get('attendance_id');
        $statistics_id = $this->request->get('statistics_id');
        $statistics_type = $this->request->get('statistics_type'); // 0-未打卡 1-已打卡
        if (is_null($statistics_type)) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '请选择统计类型');
        }
        if (!$attendance_id) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, '请选择考勤组');
        }
        $attendance = AdminAttendanceSys::where('id','=', $attendance_id)->find();
        if(!$attendance) {
            return $this->buildSuccess([
                'list' => []
            ]);
        }
        // 查统计某一天的
        if ($statistics_id) {
            $statistics_attendance = AdminStatisticsAttendance::where('id','=', $statistics_id)->find();
            if(!$statistics_attendance) {
                return $this->buildSuccess([
                    'list' => []
                ]);
            }
            $start = strtotime(date("Y-m-d",$statistics_attendance->statistics_time).' 00:00:00');
            $end = strtotime(date("Y-m-d",$statistics_attendance->statistics_time).' 23:59:59');
            \think\facade\Log::error($start);
            \think\facade\Log::error($end);
        }
        // 查实时今天的
        else {
            $start = strtotime(date("Y-m-d",strtotime("today")).' 00:00:00');
            $end = strtotime(date("Y-m-d",strtotime("today")).' 23:59:59');
        }
        $obj = AdminStaff::alias('s');
        $where = [
            ['s.type', '=', AdminStaff::STUDENT],
            ['s.status', '=', AdminStaff::AUDIT_PASS],
            ['s.is_delete', '=', 0],
            ['s.school_id', '=', $attendance->school_id]
        ];
        // 已打卡学生ID列表
        $yes_obj = AdminAttendanceRecord::where('attendance_id', '=', $attendance->id)
            ->where('create_time', '>=', $start)
            ->where('create_time', '<=', $end);
        $yes_staff = $yes_obj->group('staff_id')->column('staff_id');
        // 未打卡
        if ($statistics_type == 0) {
            $obj->where('s.id','not in', $yes_staff);
        }
        // 已打卡
        if ($statistics_type == 1) {
            $obj->where('s.id','in', $yes_staff);
        }
        $list = $obj
            ->field('s.id,s.school_id,s.student_no,s.name,s.sex,s.major,s.phone,s.create_time,s.status,s.entrance_time,s.graduate_time,s.face')
            ->group('s.id')
            ->with(['icCard'=>['student', 'dormitory']])
            ->where($where)
            ->select();
        return $this->buildSuccess([
            'list' => $list
        ]);
    }

}